package com.fenghongzhang.day017;

import androidx.appcompat.app.AppCompatActivity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void search(View view) {

        String s = "http://musicapi.leanapp.cn/search/suggest?keywords=邓紫棋";

        new SongsTask().execute(s);
    }

    class MusicTask extends AsyncTask<String ,Void,String> {


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            MusicBean musicBean = new Gson().fromJson(s, MusicBean.class);
            List<MusicBean.DataBean> data = musicBean.getData();
            for (int i = 0; i < data.size(); i++) {
                Toast.makeText(MainActivity.this,data.get(i).getUrl(), Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            URL url = null;
            try {
                url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();


                if(connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    byte[] bytes = new byte[1024];
                    StringBuilder stringBuilder = new StringBuilder();
                    int len = 0;
                    while ((len = inputStream.read(bytes)) != -1){
                        stringBuilder.append(new String(bytes, 0, len));
                    }

                    return stringBuilder.toString();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

    class SongsTask extends AsyncTask<String ,Void,String> {


        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Songs songs = new Gson().fromJson(s, Songs.class);
            List<Songs.ResultBean.SongsBean> songs1 = songs.getResult().getSongs();
            for (int i = 0; i < songs1.size(); i++) {
                Songs.ResultBean.SongsBean songsBean = songs1.get(i);
                int id = songsBean.getId();
                new MusicTask().execute("http://musicapi.leanapp.cn/music/url?id="+id);
            }
        }

        @Override
        protected String doInBackground(String... strings) {
            URL url = null;
            try {
                url = new URL(strings[0]);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();


                if(connection.getResponseCode() == 200){
                    InputStream inputStream = connection.getInputStream();
                    byte[] bytes = new byte[1024];
                    StringBuilder stringBuilder = new StringBuilder();
                    int len = 0;
                    while ((len = inputStream.read(bytes)) != -1){
                        stringBuilder.append(new String(bytes, 0, len));
                    }

                    return stringBuilder.toString();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}



